// pages/statistics/statistics.js
Page({
  data: {
    timeRange: 'month',
    mainChartData: {
      option: {
        textStyle: { color: '#333333' },
        legend: { position: 'bottom' },
        series: [{ 
          type: 'pie', 
          data: [],
          radius: ['40%', '70%'],
          label: { show: true, formatter: '{b}: {c} ({d}%)' }
        }]
      }
    },


  },
  onLoad() {
    this.loadData();
  },
  changeTimeRange(e) {
    this.setData({
      timeRange: e.currentTarget.dataset.range
    });
    this.loadData();
  },
  loadData() {
    const app = getApp();
    const records = app.globalData.records || [];
    const chartData = this.processChartData(records, this.data.timeRange);
    this.setData({
      detailList: records,
      'mainChartData.option.series[0].data': chartData.incomeExpenseData
    });
  },
  processChartData(records, range) {
    // 计算总收入和总支出
    let income = 0;
    let expense = 0;
    records.forEach(record => {
      if (record.type === 'income') {
        income += parseFloat(record.amount);
      } else if (record.type === 'expense') {
        expense += parseFloat(record.amount);
      }
    });
    // 生成收入支出饼图数据
    const incomeExpenseData = [
      { name: '收入', value: income, itemStyle: { color: '#00B42A' } },
      { name: '支出', value: expense, itemStyle: { color: '#F53F3F' } }
    ];
    return { incomeExpenseData };
  },
  handleChartZoom(e) {
    // 处理图表双指缩放
  },

});